﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string gd_Add(gd model ,int ACID);


[OperationContract]
string gd_Update(gd model ,int ACID);


[OperationContract]
string gdBypid_Delete(string pid,int ACID);


[OperationContract]
gd gdBypid_Get(string pid,int ACID );


[OperationContract]
List<gd> gd_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加gd表数据
/// <summary>
/// 添加gd
/// </summary>
/// <param name="model">gd实体</param>
/// <returns>空或错误</returns>
public string gd_Add(gd model,int ACID)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『工单ID』不能为空！";
}
if(model.opid==null || model.opid.Equals(Guid.Empty))
{
return "『操作员』不能为空！";
}
if(model.PDate ==null )
{
return "『开单日期』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.gd.Count(p=>p.pid==model.pid)>0)
{
return "主键已存在！";
}
ctx.gd.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加gd表数据事物用重构
/// <summary>
/// 添加gd事物用重构
/// </summary>
/// <param name="model">gd实体</param>
/// <returns>空或错误</returns>
private string gd_Add(gd model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『工单ID』不能为空！";
}
if(model.opid==null || model.opid.Equals(Guid.Empty))
{
return "『操作员』不能为空！";
}
if(model.PDate ==null )
{
return "『开单日期』不能为空！";
}
if(ctx.gd.Count(p=>p.pid==model.pid)>0)
{
return "主键已存在！";
}
ctx.gd.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改gd表数据
/// <summary>
/// 修改gd
/// </summary>
/// <param name="model">gd实体</param>
/// <returns>空或错误</returns>
public string gd_Update(gd model,int ACID)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『工单ID』不能为空！";
}
if(model.opid==null || model.opid.Equals(Guid.Empty))
{
return "『操作员』不能为空！";
}
if(model.PDate ==null )
{
return "『开单日期』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.gd.Count(p=>p.pid==model.pid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
gd gdmodel=ctx.gd.Single(p => p.pid==model.pid);
if(!string.IsNullOrEmpty(model.pid))
{
gdmodel.pid=model.pid;
}
if(model.PDate!=null )
{
gdmodel.PDate=model.PDate;
}
if(model.opid!=null || model.opid.Equals(Guid.Empty))
{
gdmodel.opid=model.opid;
}
if(!string.IsNullOrEmpty(model.ywno))
{
gdmodel.ywno=model.ywno;
}
if(!string.IsNullOrEmpty(model.khmc))
{
gdmodel.khmc=model.khmc;
}
if(!string.IsNullOrEmpty(model.khtel))
{
gdmodel.khtel=model.khtel;
}
if(!string.IsNullOrEmpty(model.action))
{
gdmodel.action=model.action;
}
if(!string.IsNullOrEmpty(model.cptc))
{
gdmodel.cptc=model.cptc;
}
if(!string.IsNullOrEmpty(model.cpsb1))
{
gdmodel.cpsb1=model.cpsb1;
}
if(!string.IsNullOrEmpty(model.cpsb2))
{
gdmodel.cpsb2=model.cpsb2;
}
if(!string.IsNullOrEmpty(model.xsp))
{
gdmodel.xsp=model.xsp;
}
if(!string.IsNullOrEmpty(model.fpno))
{
gdmodel.fpno=model.fpno;
}
if(!string.IsNullOrEmpty(model.clientAdd))
{
gdmodel.clientAdd=model.clientAdd;
}
if(!string.IsNullOrEmpty(model.zp))
{
gdmodel.zp=model.zp;
}
if(!string.IsNullOrEmpty(model.qszk))
{
gdmodel.qszk=model.qszk;
}
if(model.ysje!=null )
{
gdmodel.ysje=model.ysje;
}
if(model.ssje!=null )
{
gdmodel.ssje=model.ssje;
}
if(model.yhje!=null )
{
gdmodel.yhje=model.yhje;
}
if(!string.IsNullOrEmpty(model.skfs))
{
gdmodel.skfs=model.skfs;
}
if(!string.IsNullOrEmpty(model.cust1))
{
gdmodel.cust1=model.cust1;
}
if(!string.IsNullOrEmpty(model.cust2))
{
gdmodel.cust2=model.cust2;
}
if(!string.IsNullOrEmpty(model.cust3))
{
gdmodel.cust3=model.cust3;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除gd表Bypid数据
/// <summary>
/// 删除gdBypid
/// </summary>
/// <param name="pid">工单ID</param>
/// <returns>空或错误</returns>
public string gdBypid_Delete(string pid,int ACID)
{
if(string.IsNullOrEmpty(pid))
{
return "『工单ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.gd.Count(p=>p.pid==pid)!=1)
{
return null;
}
gd model=ctx.gd.Single(p => p.pid== pid);
ctx.gd.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除gd表Bypid数据[事物用重构]
/// <summary>
/// 删除gdBypid[事物用重构]
/// </summary>
/// <param name="pid">工单ID</param>
/// <returns>空或错误</returns>
private string gdBypid_Delete(string pid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(pid))
{
return "『工单ID』不能为空！";
}
if(ctx.gd.Count(p=>p.pid==pid)!=1)
{
return null;
}
gd model=ctx.gd.Single(p => p.pid== pid);
ctx.gd.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取gd表Bypid数据
/// <summary>
/// 获取gdBypid
/// </summary>
/// <param name="pid">工单ID</param>
/// <returns>当前表</returns>
public gd gdBypid_Get(string pid,int ACID)
{
if(string.IsNullOrEmpty(pid))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.gd.Count(p=>p.pid==pid)!=1)
{
return null;
}
return ctx.gd.SingleOrDefault(p=>p.pid==pid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取gd表数据
/// <summary>
/// 获取gd
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<gd></returns>
public List<gd> gd_Get(Expression<Func<gd, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.gd.ToList();
}
 else 
{
return ctx.gd.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.gd.Where(predicate).ToList();
}
 else 
{
return ctx.gd.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取gd表数据
/// <summary>
/// 获取gd
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<gd></returns>
public List<gd> gd_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return gd_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取gd表数据计数
/// <summary>
/// 获取gd表数据计数
/// </summary>
/// <returns>List<gd></returns>
public int gd_GetCount(Expression<Func<gd, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.gd.Count();

}
 else
{
return ctx.gd.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取gd分页表数据
/// <summary>
/// 获取gd分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<gd></returns>
public List<gd> gd_Getpage(int pageIndex, int pageSize,Expression<Func<gd, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.gd.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.gd.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.gd.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.gd.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改gd表数据
/// <summary>
/// 批量修改gd
/// </summary>
/// <param name="predicate">Expression<Func<gd, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<gd,gd>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int gd_batchUpdate(Expression<Func<gd, bool>> predicate,Expression<Func<gd, gd>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.gd.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改gd表数据事物用重构
/// <summary>
/// 批量修改gd事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<gd, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<gd,gd>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int gd_batchUpdate(Expression<Func<gd, bool>> predicate,Expression<Func<gd, gd>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.gd.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除gd表数据
/// <summary>
/// 批量删除gd
/// </summary>
/// <param name="predicate">Expression<Func<gd, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int gd_batchDel(Expression<Func<gd, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.gd.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除gd表数据事物用重构
/// <summary>
/// 批量删除gd事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<gd, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int gd_batchDel(Expression<Func<gd, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.gd.Delete(predicate);
return ok;
}
#endregion


}
}

